import { IPage } from "../../../IPage";

function test1() {
    /**
     * 程序创建了一个promise,这个timeout也会继续执行,完成后才会退出执行线程.
     */
    let taskA = new Promise<void>((resolve) => {
        console.log("start");
        setTimeout(() => {
            console.log("end");
            resolve();
        }, 300);
    });
}

function test2() {
    //同步执行2秒的函数.
    let taskB = (name: string) => {
        let d1 = new Date();
        let t1 = d1.getTime();
        console.log(`[${d1.toISOString()}][task start] [${name}]`)
        while (true) {
            let d2 = new Date();
            let t2 = d2.getTime();
            let dif = t2 - t1;
            if (dif >= 2000) {
                console.log(`[${d2.toISOString()}][task end] [${name}]`)
                break;
            }
        }
    }
    taskB("111");
    taskB("222");
}


function test4() {
    //promise的本质,只是将executor函数放到下一个runloop执行
    //promise本身不是多线程,只是调配了函数.阻塞的只是一个异步队列?
    //有很多队列的.
    let task = (name: string) => {
        return new Promise<void>((resolve) => {
            let d1 = new Date();
            let t1 = d1.getTime();
            console.log(`[${d1.toISOString()}][task start] [${name}]`)
            while (true) {
                let d2 = new Date();
                let t2 = d2.getTime();
                let dif = t2 - t1;
                if (dif >= 2000) {
                    console.log(`[${d2.toISOString()}][task end] [${name}]`)
                    break;
                }
            }
        });
    }
    task("111");
    task("222");
}


function test3() {
    //promise的本质,只是将executor函数放到下一个runloop执行
    //setTimout 只是将一个callback回调函数放到指定时间后执行,不阻塞.
    //同步执行2秒的函数.
    let task = (name: string) => {
        let d1 = new Date();
        let t1 = d1.getTime();
        console.log(`[${d1.toISOString()}][task start] [${name}]`)
        return new Promise<void>((resolve) => {
            setTimeout(() => {
                let d2 = new Date();
                console.log(`[${d2.toISOString()}][task start] [${name}]`)
                resolve();
            }, 2000);
        });
    }
    task("111");
    task("222");
}

test4();

module.exports = <IPage>{
    showPage: () => {
        console.log("this is page");
    }
}